<?php
/**
 * @Name: CaoBox v1.0
 * @author LinhNMT <w2ajax@gmail.com>
 * @link http://code.google.com/p/caobox/
 * @copyright Copyright &copy; 2009 phpbasic
 */
defined('_ROOT') or die(__FILE__);

$oMaster = new MasterModel;
$configure_mod = $oMaster->configure_mod();

$cache_dao = _CACHE.APPLICATION.'dao.php';
if(!file_exists($cache_dao)){
	$file_str = '<?php'."\n";
	$dir = dir(_ROOT.APPLICATION."/dao/");
	while ($file = $dir->read()) {
	  if (substr($file, -4) == '.php') {
		$class = ucfirst(substr($file,0,-4));
		$class_dao = $class.'DAO';
		require _ROOT.APPLICATION.'dao/'.$file;
		$file_str .= 'require _ROOT.APPLICATION.\''."dao/".$file."';\n";
		if(class_exists($class_dao)){
			${'o'.$class} = new $class_dao($configure_mod);
			${'o'.$class}->setCache(_CACHE);
			$file_str .= '$o'.$class.' = new '.$class_dao."(\$configure_mod);\n";
			$file_str .= '$o'.$class.'->setCache(_CACHE);'."\n";
		}
	  }
	}
	$file_str .= '?>';
	$fp = @fopen($cache_dao,'w');
	if($fp){
		fwrite($fp,$file_str);
		fclose($fp);
	}
}else{
	include $cache_dao;
}

$result = $oConfigure->view();
while($rs = $result->fetch()){
	$cfg[$rs['code']] = addslashes($rs['value']);
}
$result->cache();


$request = $_GET;



$tpl = new View('template/'.$cfg['template'],$languages); 
$tpl->folder = 'img|format|media|css|images|flash|js|upload';
$tpl->gzip = $cfg['gzip'];
$tpl->cache = _CACHE;

//Load main template
$tpl->setfile(array(
	'main'=>'master.tpl',
));

//$cache_cfg_mod = _CACHE.'configure_mod.php';
//if(!file_exists($cache_cfg_mod)){
/*	$result = $oConfigure->getMod(1);
	$file_str = "<?php\n";
	$file_str .= '$configure_mod = array();'."\n";
	while($rs = $result->fetch()){
		$data = unserialize($rs['data']);
		$file_str .= '$configure_mod[\''.$rs['module'].'\'][\''.$rs['typeid'].'\'] = array(\'languages\'=>\''.intval($data['languages']).'\');'."\n";
	}
	
	$result = $oLanguage->view(("is_default = 1");
	$default_lang = $result->fetch();
	$file_str .= '$configure_mod[\'default_lang\'] = "'.$default_lang['ln'].'";'."\n";

	$file_str .= '?>';
*//*	@unlink($cache_cfg_mod);
	$fp = @fopen($cache_cfg_mod,'w');
	if($fp){
		fwrite($fp,$file_str);
		fclose($fp);
		$hook->redirect('./');
	}else{
		$tpl->getError('We can not create file, please set <strong>WRITE</strong> access for folder <strong>'._CACHE.'</strong>');
	}
*///}


$tpl->assign(array(
	'root_dir'=>$system->root_dir,
	'_UPLOAD'=>_UPLOAD,
));
//print_r($_SERVER);
$tpl->merge((array)$system,'system');
$tpl->merge($system->modules['url'][$system->module],'switch_url');
$tpl->merge($system->modules[$system->lang]['module'],'module');
$tpl->merge($system->modules[$system->lang]['name'],'module_name');
$tpl->merge($cfg,'cfg');
$tpl->merge($languages,'lang');

$menu_active = array();
$menu_active[$system->module] = 'class="current_page_item"';
$tpl->merge($menu_active,'menu_active');

?>